<?php
/**
 * Created by PhpStorm.
 * User: 技术员专用
 * Date: 2019/3/14
 * Time: 16:21
 */

class Pro{
    private $workers;
    private $workNum = 4;

    function index(){
        $urls=[
            'https://www.baidu.com/',
            'https://www.sina.com.cn/',
            'https://www.qq.com/',
            'https://www.bilibili.com/',
        ];
        for ($i=0;$i<$this->workNum;$i++){
            $this->start($i,$urls);
        }
    }

    function start($i,$urls){
        $process=new swoole_process(function (swoole_process $worker)use($i,$urls){
            //curl
            $content=$this->curlData($urls[$i]);
            $worker->write($content);
        },true);
        $pid = $process->start();
        $this->workers[$pid] = $process;
    }

    function curlData($url){
        //模拟请求
        sleep(1);
        return $url.'---success'.PHP_EOL;
    }
    public function read(){
        foreach ($this->workers as $process){
            echo $process->read();
        }
    }
}

echo 'start:'.time().PHP_EOL;
$process=new Pro();
$process->index();
$process->read();

echo 'end:'.time().PHP_EOL;
